import { MockMethod } from 'vite-plugin-mock';

function generateUUid(length: number) {
  const str1 = Math.random().toString().slice(2);
  const str2 = Math.round(Date.now() / 1000);
  const uuid = (str1 + str2)
    .split(' ')
    .sort((a) => parseInt(a) - 0.5)
    .join('');
  return uuid.slice(0, length);
}

function generateTemplateList(length: number) {
  const list = [];
  for (let i = 0; i < length; i++) {
    const index = i + 1;
    list.push({
      id: generateUUid(16),
      cover: !i
        ? 'https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png'
        : 'https://pinia.vuejs.org/logo.svg',
      title: 'test title ' + index,
      author: 'author' + index,
      read: index
    });
  }
  return list;
}

const templateList = generateTemplateList(6);

export default [
  {
    url: '/api/template/:templateId',
    method: 'get',
    response(req: any) {
      // console.log('templateId => ', req.query.templateId);
      const { templateId } = req.query;
      const templateItem = templateList.find((item) => item.id === templateId);
      return {
        code: 0,
        msg: 'ok',
        data: templateItem
      };
    }
  },
  {
    url: '/api/templatelist',
    method: 'get',
    response() {
      // console.log('res => ', req);
      return {
        code: 0,
        msg: 'ok',
        data: templateList
      };
    }
  }
] as MockMethod[];
